package com.tabjin.rfidsocket.controller.v1.sys;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tabjin.rfidsocket.common.RequestHolder;
import com.tabjin.rfidsocket.common.utils.StringUtil;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.pojo.SysUser;
import com.tabjin.rfidsocket.pojo.bo.SysRoleBO;
import com.tabjin.rfidsocket.pojo.vo.SysRoleVO;
import com.tabjin.rfidsocket.pojo.vo.SysUserVO;
import com.tabjin.rfidsocket.service.sys.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author tabjin
 * create at 2020/12/24 00:29
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping(value = "/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysTreeService sysTreeService;
    private final SysUserService sysUserService;
    private final SysRoleUserService sysRoleUserService;
    private final SysRoleAclService sysRoleAclService;

    public SysRoleController(SysRoleService sysRoleService, SysTreeService sysTreeService, SysUserService sysUserService, SysRoleUserService sysRoleUserService, SysRoleAclService sysRoleAclService) {
        this.sysRoleService = sysRoleService;
        this.sysTreeService = sysTreeService;
        this.sysUserService = sysUserService;
        this.sysRoleUserService = sysRoleUserService;
        this.sysRoleAclService = sysRoleAclService;
    }

    @RequestMapping(value = "/index.page")
    public ModelAndView index() {
        return new ModelAndView("role");
    }

    /**
     * 新增角色
     *
     * @param sysRoleBO
     * @return TabJsonResult
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody SysRoleBO sysRoleBO) {
        SysRoleVO vo = sysRoleService.add(sysRoleBO);
        return TabJsonResult.ok(vo);
    }

    /**
     * 更新角色
     *
     * @param sysRoleBO
     * @return TabJsonResult
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody SysRoleBO sysRoleBO) {
        SysRoleVO vo = sysRoleService.update(sysRoleBO);
        return TabJsonResult.ok(vo);
    }

    /**
     * 获取角色列表
     *
     * @param name
     * @param id   角色id
     * @return TabJsonResult
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult list(@RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "id", required = false) Integer id) {
        List<SysRoleVO> voList = sysRoleService.getAll(name, id);
        return TabJsonResult.ok(voList);
    }

    /**
     * 当前角色的角色权限树
     *
     * @param roleId 角色id
     * @return TabJsonResult
     */
    @RequestMapping(value = "/roleTree", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult roleTree(@RequestParam(value = "roleId", required = false) Integer roleId) {
        SysUserVO currentUser = RequestHolder.getCurrentUser();
        return TabJsonResult.ok(sysTreeService.roleTree(roleId));
    }

    @RequestMapping(value = "/roleAcls", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult roleAclModules(@RequestParam(value = "roleId", required = true) Integer roleId) {
//        SysUserVO currentUser = RequestHolder.getCurrentUser();
        return TabJsonResult.ok(sysRoleAclService.getAclsByRoleId(roleId));
    }

    /**
     * 获取角色下所有已选用户、未选用户
     *
     * @param roleId
     * @return TabJsonResult
     */
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult users(@RequestParam(value = "roleId", required = true) Integer roleId) {
        // 已选用户列表
        List<SysUser> selectedUserList = sysRoleUserService.getUserListByRoleId(roleId);
        // 所有用户列表
        List<SysUser> allUserList = sysUserService.getAll();
        List<SysUser> unSelectedUserList = Lists.newArrayList();

        Set<Integer> selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
//            if (sysUser.getStatus() == 1 && !selectedUserList.contains(sysUser)) {// 正常状态 && 已选列表中不含当前用户
            // !selectedUserList.contains(sysUser)比较慢，用lambda表达式加速
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) {// 正常状态 && 已选列表中不含当前用户
                unSelectedUserList.add(sysUser);
            }
        }
//        selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList());
        Map<String, List<SysUser>> map = Maps.newHashMap();
        map.put("selected", selectedUserList);
        map.put("unSelected", unSelectedUserList);
        map.put("users", Stream.concat(selectedUserList.stream(), unSelectedUserList.stream()).collect(Collectors.toList()));
        return TabJsonResult.ok(map);
    }

    /**
     * 更改角色权限
     *
     * @param roleId
     * @param aclIds
     * @return
     */
    @RequestMapping(value = "changeAcls", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult changeAcls(@RequestParam(value = "roleId", required = true) Integer roleId,
                                    @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId, aclIdList);
        return TabJsonResult.ok();
    }

    /**
     * 更改角色用户
     *
     * @param roleId
     * @param userIds
     * @return
     */
    @RequestMapping(value = "/changeUsers", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult changeUsers(@RequestParam(value = "roleId", required = true) Integer roleId,
                                     @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) {
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId, userIdList);
        return TabJsonResult.ok();
    }
}
